import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Class describe Profile entity allow db access
 * @author anton
 * @version 0.01
 */
public class EW_Profiles extends DataBaseAccess {
	
	private String name = ""; // profile name
	private int askCount = 0; // ask counts for current profile
	private int restTime = 0; // rest time for current profile
	private String nativeLanguage = ""; // native language for current profile
	private String studyLanguage = ""; // study language for current profile 
	private boolean loadByDefoult = false; // mask true if this profile need to load by defoult
	
	/**
	 * Constructor for entity EW_Profile
	 * @param profileName - new profile name
	 */
	public EW_Profiles(String profileName){
		super("Profiles");
		name = profileName;
	}
	
	/**
	 * try to get profile by name
	 * @param name	-	name for search profile in DB
	 * @return
	 * 		EW_Profile </br>
	 * 		if profile find in DB then return first entity </br>
	 * 		else return null
	 */
	public EW_Profiles getProfileByName(String name){
		HashMap<String, String> query = new HashMap<String, String>();
		query.put("name", name);
		ArrayList<EW_Profiles> list = getList(query);
		if (list.size() > 0) return list.get(0);
		else return null;
	}
	
	/**
	 * get how many question should to be ask
	 * @return
	 * 		integer	-	count of questions
	 */
	public int getAskCount(){
		return askCount;
	}
	/**
	 * get how long is pause between lessons
	 * @return
	 * 		integer	-	seconds of rest
	 */
	public int getRestTime(){
		return restTime;
	}
	/**
	 * get current profile name
	 * @return
	 * 		String	-	name of profile
	 */
	public String getName(){
		return name;
	}
	/**
	 * get native language for current profile
	 * @return
	 * 		String	-	name of native language
	 */
	public String getNativeLanguage(){
		return nativeLanguage;
	}
	/**
	 * get study language for current profile
	 * @return
	 * 		String	-	name of study language
	 */
	public String getStudyLanguage(){
		return studyLanguage;
	}
	/**
	 * set new counts of questions
	 * @param count - new count
	 */
	public void setAskCount(int count){
		askCount = count;
	}
	/**
	 * set new rest period in seconds
	 * @param seconds - new seconds count for rest
	 */
	public void setRestTime(int seconds){
		restTime = seconds;
	}
	/**
	 * set new name for native language (should be in DB)
	 * @param lang - new native language in DB
	 */
	public void setNativeLanguage(EW_Languages lang){
		nativeLanguage = lang.getName();
	}
	/**
	 * set new name for study language (should be in DB)
	 * @param lang - new study language in DB
	 */
	public void setStadyLanguage(EW_Languages lang){
		studyLanguage = lang.getName();
	}
	/**
	 * set mark that this profile should load by default
	 * @param isDefault - should this profile load by default
	 */
	public void setLoadByDefoult(boolean isDefault){
		loadByDefoult = isDefault;
	}

	@Override
	protected final ResultSet getListByDetail(HashMap<?, ?> keys) {
		String selectSQL = "SELECT * From Profiles where";
		int keysCount = keys.size();
		if (keysCount == 0) {selectSQL += " 1 = 1";}
		else {
			if (keys.get("name") != null) {
				selectSQL += " name = '" +	keys.get("name") + "'";
				if (keysCount > 1) {
					selectSQL += " and ";
					keysCount--;
				}
			};
			if (keys.get("nativeLanguage") != null) {
				selectSQL += " nativeLanguage = '" +	keys.get("nativeLanguage") + "'";
				if (keysCount > 1) {
					selectSQL += " and ";
					keysCount--;
				}
			};
			if (keys.get("stadyLanguage") != null) {
				selectSQL += " stadyLanguage = '" + keys.get("stadyLanguage") + "'";
				if (keysCount > 1) {
					selectSQL += " and ";
					keysCount --;
				}
			};
			if (keys.get("loadByDefoult") != null) {
				selectSQL += " loadByDefoult = " + keys.get("loadByDefoult");
				if (keysCount > 1) {
					selectSQL += " and ";
					keysCount --;
				}
			}
		};
		
		try {
			EWDataBase db = EWDataBase.getInstance();
			return db.conn.prepareStatement(selectSQL).executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * search in DB profile which loading by default
	 * @return
	 * 		if find in db then	EW_Profile <br>
	 *  	else return null
	 */
	public EW_Profiles getProfileLoadetByDefoult(){
		HashMap<String, String> def = new HashMap<String, String>();
		def.put("loadByDefoult", "true");
		ArrayList<EW_Profiles> defList = getList(def);
		if (defList.size() >= 0){
			return defList.get(0);
		}
		else {
			return null;
		}
	}
	/**
	 * Select all profiles form DB
	 * @return
	 * 		ArrayList of EW_Profiles <br>
	 * 		if exception occur return null
	 */
	public ArrayList<EW_Profiles> getAllProfilesInDB(){
		return getList(new HashMap<String, String>());
	}
	
	/**
	 * get from db list of EW_Profiles by prepared HashMap key selections
	 * @param keys	-	selections list allow follow keys (keys sensitive) <br>
	 * 		name  <br>
	 * 		nativeLanguage <br>
	 * 		stadyLanguage <br>
	 * 		loadByDefoult <br>
	 * @return
	 * 		ArrayList of EW_Profiles <br>
	 * 		if exception occur return null
	 */	
	private ArrayList<EW_Profiles> getList(HashMap<String, String> keys){
		ArrayList<EW_Profiles> list = new ArrayList<EW_Profiles>();
		try {
			ResultSet rs = getListByDetail(keys);
			while (rs.next()) {
				EW_Profiles item = new EW_Profiles(rs.getString("name"));
				item.askCount = rs.getInt("askCount");
				item.restTime = rs.getInt("restTime");
				item.nativeLanguage = rs.getString("nativeLanguage");
				item.studyLanguage = rs.getString("stadyLanguage");
				item.loadByDefoult = rs.getBoolean("loadByDefoult");
				list.add(item);
			}
			rs.close();
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * insert current record in DB
	 * @return
	 * 		integer - result of insert
	 */
	public int insertCurRecord(){
		if (getProfileByName(name) != null) return updateCurrentRecord();
		HashMap<String, String> insertValues = new HashMap<String, String>();
		insertValues.put("name".toUpperCase(), name);
		insertValues.put("restTime".toUpperCase(), ((Integer)restTime).toString());
		insertValues.put("askCount".toUpperCase(), ((Integer)askCount).toString()); 
		insertValues.put("nativeLanguage".toUpperCase(), nativeLanguage);
		insertValues.put("stadyLanguage".toUpperCase(), studyLanguage);
		insertValues.put("loadByDefoult".toUpperCase(), ((Boolean)loadByDefoult).toString());
		return super.insertOneRecord(insertValues);
	}
	
	/**
	 * update current record in DB
	 * @return
	 * 		integer - result of update
	 */
	public int updateCurrentRecord(){
		ArrayList<String> where = new ArrayList<String>();
		where.add("name = '" + name + "'");
		
		
		ArrayList<String> sets = new ArrayList<String>();
		sets.add("restTime = " + ((Integer)restTime).toString());
		sets.add("askCount = " + ((Integer)askCount).toString()); 
		sets.add("nativeLanguage = '" + nativeLanguage + "'");
		sets.add("stadyLanguage = '" + studyLanguage + "'");
		sets.add("loadByDefoult = " + loadByDefoult);
		
		return super.updateOneRecord(where, sets);
	}
	
	/**
	 * delete profile from DB
	 * @return
	 * 		integer result of delete
	 */
	public int deleteProfile(){
		ArrayList<String> keys = new ArrayList<String>();
		keys.add("name = '" + name + "'");
		
		return super.deleteOneRecord(keys);
	}
	
	/**
	 * change status for default profile in DB
	 */
	public void setNewDefaultCurrentProfile(){
		String dropDefaultState = "UPDATE Profiles SET loadByDefoult = false WHERE loadByDefoult = true";
		String setDefaultState = "UPDATE Profiles SET loadByDefoult = true WHERE name = '" + name + "'";
		try{
		db.conn.prepareStatement(dropDefaultState).execute();
		db.conn.prepareStatement(setDefaultState).execute();
		} catch (SQLException except) {
			except.printStackTrace();
		}
	}

}
